package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GmTenantInfo;
import com.ruoyi.system.domain.ParkRate;
import com.ruoyi.system.domain.ProperFee;
import com.ruoyi.system.mapper.GmParkRateMapper;
import com.ruoyi.system.service.IGmParkRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

@Service
public class GmParkRateServiceImpl implements IGmParkRateService {
    @Autowired
    private GmParkRateMapper gmParkRateMapper;
    @Override
    public List<ParkRate> selectParkRateList(ParkRate parkRate) {
        return gmParkRateMapper.selectParkRateList(parkRate);
    }

    @Override
    public ParkRate selectParkRateById(String parkId) {
        return gmParkRateMapper.selectParkRateById(parkId);
    }

    @Override
    public int updateParkRate(ParkRate parkRate) {
//        if (parkRate.getUnitPrice().equals("")){
//            parkRate.setUnitPrice(0.0);
//        }
//        if (parkRate.getInvoicedAmount().equals("")){
//            parkRate.setInvoicedAmount(0.0);
//        }
//        if (parkRate.getAmountReceived().equals("")){
//            parkRate.setAmountReceived(0.0);
//        }
        return gmParkRateMapper.updateParkRate(parkRate);
    }

    @Override
    public int deleteParkRateByIds(String[] ids) {
        return gmParkRateMapper.deleteParkRateByIds(ids);
    }

    @Override
    public String validateDatas(List<ParkRate> lists) {
        StringBuilder sb =new StringBuilder();
        if (StringUtils.isEmpty(lists)){
            sb.append("execel中无任何数据，请认真填写！");
        }else {
            for (int i = 0; i <lists.size() ; i++) {
                if (StringUtils.isBlank(lists.get(i).getProjectName())){
                    sb.append("第"+(i+1)+"行"+"、序号为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getProjectName())){
                    sb.append("第"+(i+1)+"行"+"、项目名称为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getUnitName())){
                    sb.append("第"+(i+1)+"行"+"、单位名称为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getLicensePlateNum())){
                    sb.append("第"+(i+1)+"行"+"、车牌号不符合标准！<br/>");
                }
//                if (lists.get(i).getUnitPrice().equals("")){
//                    lists.get(i).setUnitPrice(0.0);
//                }
//                if (lists.get(i).getInvoicedAmount().equals("")){
//                    lists.get(i).setInvoicedAmount(0.0);
//                }
//                if (lists.get(i).getAmountReceived().equals("")){
//                    lists.get(i).setAmountReceived(0.0);
//                }
            }
        }
        if (sb.length()>0){
            return sb.toString();
        };
        return "";
    }

    @Override
    public String importRelations(List<ParkRate> lists, boolean updateSupport, SysUser sysUser) {
        int successNum = 0;
        int failureNum = 0;
        StringBuffer successMsg = new StringBuffer();
        StringBuffer failureMsg = new StringBuffer();
        List<ParkRate> parkRateList = gmParkRateMapper.selectAllParkRate();
        List<String> list = new ArrayList<>();
        for (ParkRate sb:lists) {
            if (StringUtils.isNotEmpty(sb.getParkId())) {
                try {
                for (ParkRate gm : parkRateList) {
                    list.add(gm.getParkId());
                }
                if (list.contains(sb.getParkId())){
                    gmParkRateMapper.updateParkRate(sb);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、" + sb.getParkId() + "导入成功");
                }else{
                    gmParkRateMapper.insertParkRate(sb);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、" + sb.getParkId() + "导入成功");
                }

            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、导入失败");
            }
            } else {
                try {
                sb.setParkId(UUID.randomUUID().toString().replaceAll("-", ""));
                gmParkRateMapper.insertParkRate(sb);
                successNum++;
                successMsg.append("<br/>" + successNum + "、" + sb.getParkId() + "导入成功");
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、导入失败");
              }
            }
        }



        if(failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共"+failureNum+"条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您数据已全部导入成功！共"+ successNum +"条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int insertParkRate(ParkRate parkRate) {
//        parkRate.setCreateTime(DateUtils.getNowDate());
//        if (parkRate.getUnitPrice().equals("")){
//            parkRate.setUnitPrice(0.0);
//        }
//        if (parkRate.getInvoicedAmount().equals("")){
//            parkRate.setInvoicedAmount(0.0);
//        }
//        if (parkRate.getAmountReceived().equals("")){
//            parkRate.setAmountReceived(0.0);
//        }
        parkRate.setParkId(UUID.randomUUID().toString().replaceAll("-", ""));
        return gmParkRateMapper.insertParkRate(parkRate);
    }

    @Override
    public List<ParkRate> selectAllParkRate() {
        return gmParkRateMapper.selectAllParkRate();
    }

    @Override
    public ParkRate selectGmParkRateById(String parkId) {
        return gmParkRateMapper.selectGmParkRateById(parkId);
    }


}
